﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Arcanea.NetworkEngine.Abstract.Engines;
using System.IO;
using System.IO.Compression;

namespace Arcanea.NetworkEngine.Concrete.Engines.Compression
{
    /// <summary>
    /// Engine de compression deflate
    /// </summary>
    public class DeflateCompressionEngine : ICompressionEngine
    {
        public override byte[] Compress(byte[] arrayUncompresed)
        {
            MemoryStream res = new MemoryStream();
            Stream str = null;
            str = new DeflateStream(res, CompressionMode.Compress, true);
            str.Write(arrayUncompresed, 0, arrayUncompresed.Length);
            str.Close();
            byte[] ret = new byte[res.Length];
            res.Seek(0, SeekOrigin.Begin);
            res.Read(ret, 0, (int)res.Length);
            res.Close();
            logger.Debug("Compress trame from " + arrayUncompresed.Length + " to " + ret.Length);
            return ret;
        }

        public override byte[] Decompress(byte[] arrayCompresed)
        {
            MemoryStream ms = new MemoryStream(arrayCompresed);
            Stream str = null;
            str = new DeflateStream(ms, CompressionMode.Decompress, true);

            byte[] tempArray = new byte[200];
            byte[] temp = null;
            int readen = 0;
            List<byte> res = new List<byte>();
            while ((readen = str.Read(tempArray, 0, tempArray.Length)) != 0)
            {
                if (tempArray.Length == readen)
                {
                    res.AddRange(tempArray);
                }
                else
                {
                    temp = new byte[readen];
                    Array.Copy(tempArray, temp, readen);
                    res.AddRange(temp);
                }
            }
            str.Close();
            logger.Debug("Decompress trame from " + arrayCompresed.Length + " to " + res.Count);
            return res.ToArray();
        }
    }
}
